package org.platform.processor.task.check.impl;

import com.mc.mic.core.tool.helper.SpringContextHelper;
import org.platform.dao.mapper.FdSysconfigMapper;
import org.platform.mo.bussmo.task.CheckItemResult;
import org.platform.processor.task.check.ICheckItem;
import org.platform.tool.util.ComUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 本类主要处理: 解调器误码率是否超过阈值
 *
 * @author FengJie
 * @date 2018/8/27
 */
public class DemodulatorBerCheckItemImpl implements ICheckItem {
    /**
     * 通道开始列
     * 开始计算通道的列
     */
    private static final int AISLE_START_COLUMN = 26 - 1;
    /**
     * 每个通道的占用的列数
     * 通道1:从 26-44 共计19列
     * 通道2:从 45-63 共计19列
     * ...
     */
    private static final int AISLE_COLUMN_COUNT = 19;

    /**
     * 帧头误码率 所在列数
     */
    private static final int FRAME_HEADER_COLUMN = 43 - 1;
    /**
     * 全帧误码率 所在列数
     */
    private static final int FRAME_COLUMN = 44 - 1;

    /**
     * 帧头误码率数据表key
     */
    private static final String FRAME_HEADER_BER_VALUE_KEY = "frameHeaderBerValue";
    /**
     * 全帧误码率数据表key
     */
    private static final String FRAME_BER_VALUE_KEY = "frameBerValue";

    private FdSysconfigMapper sysConfigMapper;

    public DemodulatorBerCheckItemImpl() {
        this.sysConfigMapper = SpringContextHelper.getBean(FdSysconfigMapper.class);
    }

    @Override
    public CheckItemResult getCheckResult(String taskSerialNumber, String jobTaskID, File file) throws IOException {
        CheckItemResult result = new CheckItemResult(true, "正常");

        double frameHeadValue = Double.valueOf(sysConfigMapper.selectByPrimaryKey(FRAME_HEADER_BER_VALUE_KEY).getCurrentvalue());
        double frameValue = Double.valueOf(sysConfigMapper.selectByPrimaryKey(FRAME_BER_VALUE_KEY).getCurrentvalue());
        double count = 0;

        FileInputStream fileInputStream = new FileInputStream(file);
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
        BufferedReader reader = new BufferedReader(inputStreamReader);
        Integer lines = ComUtils.longToInteger(reader.lines().count());
        //记录每个通道的错误次数     key:通道号,v:错误的次数
        Map<Integer, Double> frameHeaderCountMap = new HashMap<>(lines);
        Map<Integer, Double> frameCountMap = new HashMap<>(lines);

        //第一行为标题
        reader.readLine();

        String line;
        while ((line = reader.readLine()) != null) {
            ++count;

            String[] split = line.split("\t");
            //i等于当前的通道数   (通道数= (所有列数-公共部分列数)/每个通道的列数 )
            for (int i = 1; i <= (split.length - AISLE_START_COLUMN) / AISLE_COLUMN_COUNT; i++) {
                //计算每个通道 载波/位同步/帧同步 的值:  公共部分列数+ 通道数*(载波/位同步/帧同步第一通道列数-公共部分列数)
                if (!frameHeaderCountMap.containsKey(i)) {
                    frameHeaderCountMap.put(i, 0D);
                }
                double v = Double.parseDouble(split[AISLE_START_COLUMN + i * (FRAME_HEADER_COLUMN - AISLE_START_COLUMN)]);
                frameHeaderCountMap.put(i, frameHeaderCountMap.get(i) + v);

                if (!frameCountMap.containsKey(i)) {
                    frameCountMap.put(i, 0D);
                }
                double v1 = Double.parseDouble(split[AISLE_START_COLUMN + i * (FRAME_COLUMN - AISLE_START_COLUMN)]);
                frameCountMap.put(i, frameCountMap.get(i) + v1);
            }
        }
        reader.close();
        inputStreamReader.close();
        fileInputStream.close();

        List<Integer> l1 = new ArrayList<>();
        List<Integer> l2 = new ArrayList<>();

        //判断是否超过预设的阈值
        final double c = count;
        frameHeaderCountMap.forEach((channel, sum) -> {
            if (sum / c > frameHeadValue) {
                result.setStatus(false);
                l1.add(channel);
            }
        });
        frameCountMap.forEach((channel, sum) -> {
            if (sum / c > frameValue) {
                result.setStatus(false);
                l2.add(channel);
            }
        });

        //输入最后所有通道的异常信息
        StringBuffer b1 = new StringBuffer();
        l1.forEach(channel -> b1.append(channel).append(","));
        String e1 = b1.toString();
        if (e1.length() > 1) {
            e1 = e1.substring(e1.length() - 1);
            result.setConclusion("通道" + e1 + " 解调器帧头误码率异常");
        }

        StringBuffer b2 = new StringBuffer();
        l2.forEach(channel -> b2.append(channel).append(","));
        String e2 = b2.toString();
        if (e2.length() > 1) {
            e2 = e2.substring(e2.length() - 1);
            result.setConclusion(result.getConclusion() + "; 通道" + e2 + "解调器全帧误码率异常");
        }

        return result;
    }
}
